package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbContentCategoryMapper;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.pojo.TbContentExample;
import com.dongdongshop.service.ContentCategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class ContentCategoryServiceImpl implements ContentCategoryService {
    @Resource
    private TbContentCategoryMapper tbContentCategoryMapper;
    @Resource
    private TbContentMapper         tbContentMapper;
    @Override
    public List<TbContentCategory> getContentCategoryAll() {
        return tbContentCategoryMapper.selectByExample(null);
    }

    @Override
    public int addContentCategory(TbContentCategory contentCategory) {
        int insert = tbContentCategoryMapper.insert(contentCategory);
        return insert;
    }

    @Override
    public TbContentCategory addContentCategoryEcho(Long id) {
        TbContentCategory contentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
        return contentCategory;
    }

    @Override
    public int updateContentCategory(TbContentCategory contentCategory) {
        int i = tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
        return i;
    }

    @Override
    public int deleteContentCategory(Long id) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        List<TbContent> tbContents = tbContentMapper.selectByExample(example);
        int i = 0;
        if (tbContents.size() == 0){
            i = tbContentCategoryMapper.deleteByPrimaryKey(id);
        }else {
            i = 2;
        }
        return i;
    }
}
